package com.wn.service.impl;

import com.wn.dao.*;
import com.wn.entity.*;
import com.wn.service.OrderItemService;
import com.wn.utils.Rsp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (OrderItem)表服务实现类
 *
 * @author makejava
 * @since 2020-10-26 09:32:24
 */
@Service("orderItemService")
public class OrderItemServiceImpl implements OrderItemService {
    @Resource
    private OrderItemDao orderItemDao;
    @Resource
    private GuideinfoDao guideinfoDao;
    @Resource
    private HotelinfoDao hotelinfoDao;
    @Resource
    private CateringDao cateringDao;
    @Resource
    private RoutesDao routesDao;
    @Resource
    private ScenicDao scenicDao;
    @Resource
    private OrderDao orderDao;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public OrderItem queryById(String id) {
        return this.orderItemDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<OrderItem> queryAllByLimit(int offset, int limit) {
        return this.orderItemDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param orderItem 实例对象
     * @return 实例对象
     */
    @Override
    public OrderItem insert(OrderItem orderItem) {
        this.orderItemDao.insert(orderItem);
        return orderItem;
    }

    /**
     * 修改数据
     *
     * @param orderItem 实例对象
     * @return 实例对象
     */
    @Override
    public OrderItem update(OrderItem orderItem) {
        this.orderItemDao.update(orderItem);
        return this.queryById(orderItem.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.orderItemDao.deleteById(id) > 0;
    }

    @Override
    public Rsp addOneOrderItem(OrderItem orderItem) {
        int insert = this.orderItemDao.insert(orderItem);
        if(insert>0){
            return Rsp.success(orderItemDao.queryById(orderItem.getId()));
        }else {
            return  Rsp.fail();
        }

    }

    @Override
    public Rsp queryByOrderNo(OrderItem orderItem1) {
        OrderItem orderItem = orderItemDao.queryByOrderNo(orderItem1.getOrderid());
        System.out.println(orderItem.getGuideid());
        if(orderItem.getGuideid()!=null){
            Guideinfo guideinfo = guideinfoDao.queryById(orderItem.getGuideid());
            orderItem.setGuideinfo(guideinfo);
        }
        if(orderItem.getCateringid()!=null){
            Catering catering = cateringDao.queryById(orderItem.getCateringid());
            orderItem.setCatering(catering);
        }
        if(orderItem.getHotelid()!=null){
            Hotelinfo hotelinfo = hotelinfoDao.queryById(orderItem.getHotelid());
            orderItem.setHotelinfo(hotelinfo);
        }
        if(orderItem.getRoutesid()!=null){
            Routes routes = routesDao.queryById(orderItem.getRoutesid());
            orderItem.setRoutes(routes);
        }
        if(orderItem.getScenicid()!=null){
            Scenic scenic = scenicDao.queryById(orderItem.getScenicid());
            orderItem.setScenic(scenic);
        }if(orderItem.getOrderid()!=null){
            Order order = orderDao.queryById(orderItem.getOrderid());
            orderItem.setOrder(order);
        }
        System.out.println(orderItem);
        return Rsp.success(orderItem);
    }
}